package appapi.controller.oider;

import appapi.Request.*;
import appapi.Request.Basic.DictionaryPageReq;
import appapi.Request.Basic.DictionaryReq;
import appapi.Vo.AddModuleVo;
import appapi.Vo.EditModuleVo;
import appapi.Vo.GetEquipOperPageVo;
import appapi.Vo.basic.AddDictVo;
import appapi.common.ResponseBean;
import appapi.entity.Zhongjiao.DictionaryDto;
import appapi.entity.auth.ModulelistEntity;
import appapi.service.IDictionaryService;
import appapi.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 数据字典接口分拆
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/06/02 14:00
 */
@Api(value = "/api/dictionary", tags = "数据字典")
@RestController
@RequestMapping(value = "/api/dictionary")
public class DictionaryController {

    @Resource
    private IDictionaryService iDictionaryService;

    /**
     * 数据字典查询
     *
     * @return
     * @author djq 2023/5/25
     */
    @ApiOperation(value = "数据字典查询", notes = "数据字典查询")
    @GetMapping(value = "/GetDictionary")
    public ResponseBean GetDictionary(@Valid String DicType) {
        try {

            List<Map<String, Object>> list = iDictionaryService.selectDicTypeList(DicType);

            return ResponseBean.success(list);
        } catch (Exception e) {
            return ResponseBean.fail("数据字典查询异常");
        }
    }

    /**
     * 分页列表
     */
    @GetMapping("/getListByPage")
    @ApiOperation(value = "分页列表")
    public ResponseBean<DictionaryDto> GetPageList(@Valid DictionaryPageReq req) {
        try {
            if (req.getCurrPage() == null || req.getPageSize() == null) {
                req.setCurrPage(1);
                req.setPageSize(10);
            }
            PageInfo<DictionaryDto> pageInfo = iDictionaryService.Page(req);
            return ResponseBean.success(pageInfo);
        } catch (Exception e) {
            return ResponseBean.fail("分页获取信息异常");
        }

    }
    /**
     * 数据字典导出
     *
     * @return
     * @auther ygp 2023/6/12
     */
    @ApiOperation(value = "数据字典导出", notes = "数据字典导出接口", produces = "application/octet-stream")
    @GetMapping(value = "/DictionaryRecExport")
    @ApiOperationSupport(ignoreParameters = {"currPage", "pageSize"})
    public ResponseBean DictionaryRecExport(DictionaryPageReq req, HttpServletResponse response) {
        try {
            List<DictionaryDto> list = iDictionaryService.getList(req);
            if (list.size() == 0) {
                return ResponseBean.success("数据源为空");
            } else if (list.size() > 5000) {
                return ResponseBean.fail("记录超过5000，请重新选择查询条件");
            } else {
                ExcelUtils.exportExcel(list, "数据字典", "数据字典sheet", DictionaryDto.class, "数据字典", response);
                return ResponseBean.success();
            }
        } catch (Exception e) {
            return ResponseBean.fail("数据字典导出异常");
        }
    }
    /**
     * 选择列表
     */
    @GetMapping("/getList")
    @ApiOperation(value = "获取列表")
    public ResponseBean<DictionaryDto> list(@Valid DictionaryReq req) {
        try {
            List<DictionaryDto> dataList = iDictionaryService.GetList(req);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            return ResponseBean.fail("获取列表异常");
        }
    }

    /**
     * 类别列表
     */
    @GetMapping("/getTypeList")
    @ApiOperation(value = "获取类型列表")
    public ResponseBean<String> Typelist() {
        try {
            List<String> dataList = iDictionaryService.GetTypeList();
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            return ResponseBean.fail("获取类别列表异常");
        }
    }


    /**
     * 信息
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取详情")
    public ResponseBean<DictionaryDto> detail(Long dictId) {
        try {
            if (dictId == null) {
                return ResponseBean.fail("ID不能为空");
            }
            DictionaryDto entity = iDictionaryService.GetDetail(dictId);
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("获取详情失败");
            }
        } catch (Exception e) {
            return ResponseBean.fail("获取详情异常");
        }
    }

    /**
     * 新增
     */
    @PostMapping(value = "/addInfo")
    @ApiOperation(value = "新增信息")
    public ResponseBean insert(@Validated @RequestBody AddDictVo vo) {
        try {
            DictionaryDto entity = vo.vo2Entity();
            entity.setDictState(0);
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            int count = iDictionaryService.Count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复增加");
            }
            int iRet = iDictionaryService.AddInfo(entity);
            if (iRet > 0) {
                return ResponseBean.success("保存成功");
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (Exception e) {
            return ResponseBean.fail("添加异常");
        }
    }

    /**
     * 修改
     */
    @PostMapping(value = "/editInfo")
    @ApiOperation(value = "修改信息")
    public ResponseBean update(@Validated @RequestBody AddDictVo vo) {
        DictionaryDto entity = vo.vo2Entity();
        try {
            entity.setUpdateTime(LocalDateTime.now());
            int count = iDictionaryService.Count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已经存在，不能重复修改");
            }
            int iRet = iDictionaryService.EditInfo(entity);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            return ResponseBean.fail("更新信息异常");
        }
    }

    /**
     * 删除
     */
    @GetMapping(value = "/deleteInfo/{dictId}")
    @ApiOperation(value = "删除信息")
    public ResponseBean delete(@PathVariable Long dictId) {
        try {
            if (dictId == null) {
                return ResponseBean.fail("ID不能为空");
            }
            int iRet = iDictionaryService.DeleteInfo(dictId);
            if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception e) {
            return ResponseBean.fail("删除异常");
        }
    }
}
